var http = require('http');
var express = require('express');
var querystring = require('querystring');
const modeUrl = require("url");
var axios = require('axios');
var app = express();
var https = require("https");  
var jsdom = require('jsdom');
const { JSDOM } = jsdom;
// var cookieTool = require('./cookieTool.js')

axios.defaults.withCredentials = true
const zdmHost = 'm.smzdm.com'

// function nodePostGetRequest(HOST, PORT, method, bodydata, callBackFunction, path, cookie) {
//     //把将要发送的body转换为json格式 
//  var body = bodydata;
//  var bodyString = JSON.stringify(body);
//     //http 头部
//  var headers = {
//   'Content-Type': 'application/json',
//   'Content-Length': bodyString.length,
//   'Cookie': cookie
//  };

// //用与发送的参数类型
// var options = {
// host: HOST,    //ip
// port: PORT,     //port
// path: path,     //get方式使用的地址
// method: method, //get方式或post方式
// headers: headers
//    };
//    var req = http.request(options, function(res) {
// res.setEncoding('utf-8');

// var responseString = '';

// res.on('data', function(data) {
//     responseString += data;
// });

// res.on('end', function() {
//     //这里接收的参数是字符串形式,需要格式化成json格式使用
//     var resultObject = JSON.parse(responseString);
//     console.log('-----resBody-----', resultObject);
//     callBackFunction(responseString);
// });

// req.on('error', function(e) {
//     console.log('-----error-------', e);
// });
//  });
//  req.write(bodyString);
// req.end();
// }

// function nodePostGetRequest(HOST, PORT, method, bodydata, callBackFunction, path, cookie) {
//     //把将要发送的body转换为json格式 
//     var body = bodydata;
//     var bodyString = JSON.stringify(body);
//     //http 头部
//     var headers = {
//         'Content-Type': 'application/json',
//         'Content-Length': bodyString.length,
//         'Cookie': cookie
//     };

//     //用与发送的参数类型
//     var options = {
//         host: HOST,    //ip
//         port: PORT,     //port
//         path: path,     //get方式使用的地址
//         method: method, //get方式或post方式
//         headers: headers
//     };
//     var req = http.request(options, function(res) {
//         res.setEncoding('utf-8');
//         var responseString = '';

//         res.on('data', function(data) {
//             responseString += data;
//         });

//         res.on('end', function() {
//             //这里接收的参数是字符串形式,需要格式化成json格式使用
//             var resultObject = JSON.parse(responseString);
//             console.log('-----resBody-----', resultObject);
//             callBackFunction(responseString);
//         });

//         req.on('error', function(e) {
//             // TODO: handle error.
//             console.log('-----error-------', e);
//         });
//     });
//     req.write(bodyString);
//     req.end();
// }

// app.get('/apizdm/homepage', function (reqExp, resExp) {
  // let targetUrl = url.replace("https","http");
  // let urlObj = modeUrl.parse(targetUrl);
  // const options = {
  //   hostname: urlObj.host,
  //   port: 80,
  //   path: '/',
  //   method: 'GET',
  //   headers: {
  //     referer: '',
  //     Host: ''
  //   }
  // };
  // var request = http.request(options,function(response){
  //   let mSetCookie = response.headers['set-cookie']
  //   console.log(mSetCookie)
  //   resExp.statusCode = response.statusCode;
  //   response.pipe(resExp);
  // }).on("error",function(){
  //   resExp.statusCode = 503;
  //   resExp.end();
  // });
  // reqExp.pipe(request); 
// })

// 获取 什么值得买主页 home-list
// 


// app.get('/apizdm/home-list2', function (req, res) {

//   var tmpCookie = cookieTool.getCookieObj(zdmHost)

//   let urlObj = modeUrl.parse(req.url);
//   // let setTime = Date.now()
//   // setTime = String(setTime - 45499070)
//   // setTime = setTime.substr(0, setTime.length - 2)
//   var url = `https://m.smzdm.com/ajax_home_list_show?timesort=${req.query.timesort}&past_num=${req.query.past_num}`
//   var nUrl = modeUrl.parse(url)
  
//   if (!tmpCookie || !tmpCookie['__ckguid']) {
//     // 第一次请求，先获取cookie
//     getFirstLoad().then((resFirst) => {

//       let mSetCookie = resFirst.headers['set-cookie']
//       // let mExpires = resFirst.headers['expires']
//       let newCookieObj = cookieTool.normalCookie(mSetCookie)
//       cookieTool.setCookieObj(zdmHost, newCookieObj)
//     }).then((resSecond) => {

//       // url = 'https://m.smzdm.com/ajax_home_list_show?timesort=154530918237&past_num=200'
//       dealZdmAjax(url, req, res)
//     })
//   } else {
//     // url = 'https://m.smzdm.com/ajax_home_list_show?timesort=154530918237&past_num=200'
//     dealZdmAjax(url, req, res)
//   }
// });

// function getFirstLoad() {
//   console.log('to get first load')
//   return new Promise((resove, reject) => {
//     let url = 'https://m.smzdm.com/'
//     axios.get(url, {
//       headers: {
//         Host: 'm.smzdm.com',
//         Referer: 'https://m.smzdm.com/'
//       }
//     }).then((response) => {
//       // console.log(response)
//       // let mSetCookie = response.headers['set-cookie']
//       // let mUrl = modeUrl.parse(url)
//       // let newCookieObj = cookieTool.normalCookie(mSetCookie)
//       // console.log(newCookieObj)
//       // cookieTool.setCookieObj(mUrl.host, newCookieObj)
//       // resExp.send(response.data)
//       resove(response)
//     }).catch((e) => {
//       console.log('catch')
//       console.log(e)
//       reject(e)
//     })

//   })
// }

// 工具方法 ,访问张大妈首页，获取cookie并保存cookie
// function dealZdmAjax(url, req, res) {
//     var mCookie = cookieTool.getCookieObj(zdmHost)
//     let sendCookie = cookieTool.formateCookieToString(mCookie) 
//     axios.get(url, {
//       headers: {
//         Host: 'm.smzdm.com',
//         Referer: 'https://m.smzdm.com/',
//         Cookie: sendCookie
//       }
//     }).then((response) => {
//       var ret = response.data
//       if (typeof ret === 'string') {
//         var reg = /^\w+\(({.+})\)$/
//         var matches = ret.match(reg)
//         if (matches) {
//           ret = JSON.parse(matches[1])
//         }
//       }
//       res.json(ret)
//     }).catch((e) => {
//       console.log(e)
//     })
// }



// // 获取 什么值得买主页 home-list
// app.get('/apizdm/home-list1', function (req, res) {
//   // timesort=1545284108340&past_num=80
//   let urlObj = modeUrl.parse(req.url);
//   // console.log(req.query) 
//   // console.log(req.search)
//   var url = `https://m.smzdm.com/ajax_home_list_show?timesort=${Date.now()}&past_num=${req.query.past_num}`

//   axios.get(url, {
//     headers: {
//       Host: 'm.smzdm.com',
//       Referer: 'https://m.smzdm.com/'
//     }
//   }).then((response) => {

//     // console.log('response')
//     // console.log(response)

//     var ret = response.data
//     if (typeof ret === 'string') {
//       var reg = /^\w+\(({.+})\)$/
//       var matches = ret.match(reg)
//       if (matches) {
//         ret = JSON.parse(matches[1])
//       }
//     }
//     res.json(ret)
//   }).catch((e) => {
//     console.log(e)
//   })
// });

app.get('/apizdm/home-list', function (req, res) {
  var url = `https://m.smzdm.com/ajax_home_list_show?timesort=${req.query.timesort}&past_num=${req.query.past_num}`
  axios.get(url, {
    headers: {
      Host: 'm.smzdm.com',
      Referer: 'https://m.smzdm.com/'
    }
  }).then((response) => {
    var ret = response.data
    if (typeof ret === 'string') {
      var reg = /^\w+\(({.+})\)$/
      var matches = ret.match(reg)
      if (matches) {
        ret = JSON.parse(matches[1])
      }
    }
    res.json(ret)
  }).catch((e) => {
    console.log(e)
  })
})

// 图片获取代理
app.get('/apizdm/getimg', function (reqExp, resExp) {
  // var url = 'http://y.zdmimg.com/201812/13/5c12336a4539e7760.jpg_a200.jpg'
  // console.log(reqExp.query)
  let targetUrl = reqExp.query.url.replace("https","http");
  let urlObj = modeUrl.parse(targetUrl);
  const options = {
    hostname: urlObj.host,
    port: 80,
    path: urlObj.path,
    method: 'GET',
    headers: {
      referer: '',
      Host: ''
      // 'Content-Type': 'application/x-www-form-urlencoded',
      // 'Content-Length': Buffer.byteLength(postData)
    }
  };
  var request = http.request(options,function(response){
    resExp.statusCode = response.statusCode;
    response.pipe(resExp);
  }).on("error",function(){
    resExp.statusCode = 503;
    resExp.end();
  });
  reqExp.pipe(request); 
});

// 首页 swiper数据 ，需要jsdom分析
app.get('/apizdm/home-swiper', function (req, resExp) {

  var url='https://m.smzdm.com';
  https.get(url, function (res) {  
    var datas = [];  
    var size = 0;  
    res.on('data', function (data) {  
        datas.push(data);  
        size += data.length;  
    //process.stdout.write(data);  
    });  
    res.on("end", function () {  
      var buff = Buffer.concat(datas, size);  
      // var result = iconv.decode(buff, "utf8"); //转码//var result = buff.toString();//不需要转编码,直接tostring  
      var result = buff.toString()
      const dom = new JSDOM(result);
      var tagAs = dom.window.document.querySelectorAll(".swiper-container a")
      var imgSrcs = []  // [{href: '', imgSrc: 'http...'}... ]
      for(let i=0;i<tagAs.length;i++) {
        let tHref = tagAs[i].href
        let tImgSrc = tagAs[i].querySelector('img').src
        imgSrcs.push({
          href: tHref,
          imgSrc: tImgSrc
        })
      }
      resExp.json({message: 1, data: imgSrcs})
    });  
  }).on("error", function (err) {  
      Logger.error(err.stack)  
      // callback.apply(null);  
  });  

})

// 抓取article详情页数据， 因为返回的是HTML，所以需要解析
app.get('/apizdm/pdetail', function (reqExp, resExp) {

  let articleid = reqExp.query.articleid
  // console.log(articleid)
  var url='https://m.smzdm.com/p/' + articleid + '/';
  https.get(url, function (res) {  
    var datas = [];  
    var size = 0;  
    res.on('data', function (data) {  
        datas.push(data);  
        size += data.length;  
    });  
    res.on("end", function () {  
      var buff = Buffer.concat(datas, size);  
      // var result = iconv.decode(buff, "utf8"); //转码//var result = buff.toString();//不需要转编码,直接tostring  
      var result = buff.toString()
      // console.log(result)
      const dom = new JSDOM(result);
      let title=desc=worth=unWorth=favorite=comments=''
      let article_mall=last_update_time=article_referrals=''
      try {
        var articleDom = dom.window.document.querySelector(".article")
        if (articleDom  && articleDom.querySelector) {
          title = articleDom.querySelector('.title-box').textContent
          desc = articleDom.querySelectorAll('p')[1].textContent    
        }
      } catch(err) {
        console.log(err)
      }

      try {
        var worthDom = dom.window.document.querySelectorAll(".worth-section a")
        if (worthDom  && worthDom.length > 0) {
          worth = worthDom[0].querySelector('span').textContent 
          unWorth = worthDom[1].querySelector('span').textContent   
        }
      } catch(err) {
        console.log(err)
      }
      // 
      try {
        var fotterDom = dom.window.document.querySelector(".footer-wrap")
        if (fotterDom && fotterDom.querySelector) {
          favorite = fotterDom.querySelector('.zm-icon-star-o ~ span').textContent
          comments = fotterDom.querySelector('.zm-icon-comments-o ~ span').textContent
        }
      } catch(err) {
        console.log(err)
      }

      try {
        let titleDom = dom.window.document.querySelector(".details-title")
        if (titleDom && titleDom.querySelector) {
          article_mall = titleDom.querySelector('.grey span.mall').textContent
          last_update_time = titleDom.querySelector('.grey span.time').textContent
          article_referrals = titleDom.querySelector('.grey span.author').textContent
        }
      } catch(err) {
        console.log(err)
      }

      var reJson = {
        message: 1,
        data: {
          title: title,
          desc: desc,
          worth: worth,
          unWorth: unWorth,
          favorite: favorite,
          comments: comments,
          article_mall,
          last_update_time,
          article_referrals
        }
      }
      resExp.json(reJson)

    });  
  }).on("error", function (err) { 
    resExp.json({})
    Logger.error(err.stack)  
      // callback.apply(null);  
  });  
})

// 获取热门推荐数据
app.get('/apizdm/hot-recommend', function (req, res) {
  // https://m.smzdm.com/ajax_hot_recommend?timesort=1545460224&channel_id=1&article_id=12047931&device_id=1698774024154479408763137587e2d3b8990cc3d1c1a34adde5f9c3
  var url = `https://m.smzdm.com/ajax_hot_recommend?timesort=${req.query.timesort}&channel_id=${req.query.channel_id}&article_id=${req.query.article_id}`
  axios.get(url, {
    headers: {
      Host: 'm.smzdm.com',
      Referer: 'https://m.smzdm.com/'
    }
  }).then((response) => {
    var ret = response.data
    res.json(ret)
  }).catch((e) => {
    res.json({})
    console.log(e)
  })
})

// 获取好价
app.get('/apizdm/good-price', function (req, res) {
  // https://m.smzdm.com/ajax_hot_recommend?timesort=1545460224&channel_id=1&article_id=12047931&device_id=1698774024154479408763137587e2d3b8990cc3d1c1a34adde5f9c3
  var url = `https://faxian.m.smzdm.com/ajax_faxian_list_show?timesort=${req.query.timesort}`
  axios.get(url, {
    headers: {
      Host: 'faxian.m.smzdm.com',
      Referer: 'https://faxian.m.smzdm.com'
    }
  }).then((response) => {
    var ret = response.data
    res.json(ret)
  }).catch((e) => {
    res.json({})
    console.log(e)
  })
})


// 获取查询结果详情内容，返回  {message: 1, data: [{article_id: 123},...]}
app.get('/apizdm/search-result-detail', function (reqExp, resExp) {

  let s = reqExp.query.s
  // console.log(articleid)
  var url='https://search.m.smzdm.com/?s=' + encodeURI(s) +'&source=sug&v=b'

  https.get(url, function (res) {  
    var datas = []
    var size = 0
    res.on('data', function (data) {  
        datas.push(data)
        size += data.length
    });  
    res.on("end", function () {  
      var buff = Buffer.concat(datas, size);  
      // var result = iconv.decode(buff, "utf8"); //转码//var result = buff.toString();//不需要转编码,直接tostring  
      var result = buff.toString()
      const dom = new JSDOM(result, { runScripts: "dangerously" })
      let ret = []
      // //m.smzdm.com/p/12082201/
      let reg = /^\S+\/p\/(\S+)\/$/
      var wrapContentDom = dom.window.document.querySelector(".wrap-content")
      try {
        if (wrapContentDom && wrapContentDom.querySelector) {
          let groupListItemDomArr = wrapContentDom.querySelectorAll('li.card-group-list')
          for (let j = 0; j < groupListItemDomArr.length; j++) {
            let article_url=article_id=article_pic=article_title=article_price=article_mall=article_date=article_comment=article_worthy=''
            groupListItemDom = groupListItemDomArr[j]
            let aItemDom = groupListItemDom.querySelector('a')
            article_url = aItemDom.href
            // 获取 article_id
            let matchs = article_url.match(reg)
            if(matchs && matchs[1]) {
              article_id = matchs[1]
            }
            let zmCardDom = aItemDom.querySelector('.zm-card')
            let imgDom = zmCardDom.querySelector('.zm-card-media img')
            let titleDom = zmCardDom.querySelector('.zm-card-content .zm-card-title')
            let priceDom = zmCardDom.querySelector('.zm-card-content .card-price')
            let mallDomArr = zmCardDom.querySelectorAll('.zm-card-actions .zm-card-actions-left span span')
            let commontsDomArr = zmCardDom.querySelectorAll('.zm-card-actions .zm-card-actions-right .icon-group')

            if (imgDom) article_pic = imgDom.src
            if (titleDom) article_title = titleDom.textContent
            if (priceDom) article_price = priceDom.textContent.replace(/[\r\n]/g,"").trim()
            if (mallDomArr && mallDomArr.length > 1) {
              article_mall = mallDomArr[0].textContent.trim()
              article_date = mallDomArr[1].textContent.trim()
            }
            if (commontsDomArr && commontsDomArr.length > 1) {
              article_comment = commontsDomArr[0].textContent.trim()
              article_worthy = commontsDomArr[0].textContent.trim()
            }
            //console.dir(aItemDom.onclick.toString())
            if (article_id) {
              ret.push({
                article_url: article_url,
                article_id: article_id,
                article_pic: article_pic,
                article_title: article_title,
                article_price: article_price,
                article_mall: article_mall,
                article_date: article_date,
                article_comment: article_comment,
                article_worthy: article_worthy
              })
            }
          }
        }
      } catch (e) {
        console.log('getSearchResult occur error')
        console.log(e)
      }
      var reJson = {
        message: 1,
        data: ret
      }
      resExp.json(reJson)
    });  
  }).on("error", function (err) {  
    Logger.error(err.stack)  
    resExp.json({})
  });  

})






// 抓取分类详情页， 因为返回的是HTML，所以需要解析
app.get('/apizdm/fenleidetail', function (reqExp, resExp) {

  let targetUrl = reqExp.query.url
  https.get(targetUrl, function (res) {  
    var datas = []
    var size = 0
    res.on('data', function (data) {  
        datas.push(data)
        size += data.length
    });  
    res.on("end", function () {  
      var buff = Buffer.concat(datas, size);  
      // var result = iconv.decode(buff, "utf8"); //转码//var result = buff.toString();//不需要转编码,直接tostring  
      var result = buff.toString()
      const titleData={}
      const categoryTags=[],starProducts=[],hotBrands=[],youhuiList=[]
      const dom = new JSDOM(result)
      // 获取 titleData
      try{
        let titleMainDom = dom.window.document.querySelector("#category")
        let titleBrandImgDom = titleMainDom.querySelector(".brand-img img")
        titleData.brandImg = titleBrandImgDom.src || ''
        let brandLogoDom = titleMainDom.querySelector(".brand-logo img")
        titleData.brandLogo = brandLogoDom.src || ''
        let brandNameDom = titleMainDom.querySelector(".brand-title-info .brand-name")
        titleData.brandName = brandNameDom.innerHTML.split('<span')[0] || ''
        let brandAttentionDom = titleMainDom.querySelector(".brand-title-info .attention-num span")
        titleData.attentionNum = brandAttentionDom.innerHTML.split('<span')[0] || ''

      } catch(e) {
        console.log('get titleData occur error')
        console.log(e)
      }
      // console.log(titleData)

      // 获取categoryTags
      try{
        let categoryTagsDom = dom.window.document.querySelector(".category-tags")
        let tagsAs = categoryTagsDom.querySelectorAll(".swiper-wrapper .swiper-slide a")
        if (tagsAs.length > 0) {
          for(let i=0; i<tagsAs.length; i++) {
            categoryTags.push({
              tagName: tagsAs[i].textContent.replace(/[\r\n]/g,"").trim(),
              href: tagsAs[i].href
            })
          }
        }
      } catch(e) {
        console.log('get categoryTags occur error')
        console.log(e)
      }
      // console.log(categoryTags)


      // 获取starProducts
      try{
        let starProductsDom = dom.window.document.querySelector(".star-products")
        let starProductsAs = starProductsDom.querySelectorAll(".swiper-wrapper .swiper-slide a")
        if (starProductsAs.length > 0) {
          for(let i=0; i<starProductsAs.length; i++) {
            // 获取img
            let imgDom = starProductsAs[i].querySelector(".img-radius img")
            let titeAndPrice = starProductsAs[i].querySelectorAll(".card-info p")
            starProducts.push({
              href: starProductsAs[i].href,
              imgSrc: imgDom.src,
              title: titeAndPrice[0].textContent,
              price: titeAndPrice[1].textContent
            })
          }
        }
      } catch(e) {
        console.log('get starProducts occur error')
        console.log(e)
      }
      // console.log(starProducts)

      // 获取hotBrands
      try{
        let hotBrandsMainDom = dom.window.document.querySelector(".hot-brands")
        let hotBrandAs = hotBrandsMainDom.querySelectorAll(".swiper-wrapper .swiper-slide a")
        if (hotBrandAs.length > 0) {
          for(let i=0; i<hotBrandAs.length; i++) {
            // 获取img
            let imgDom = hotBrandAs[i].querySelector(".card-img img")
            let titleDom = hotBrandAs[i].querySelector(".card-info .card-title")
            hotBrands.push({
              href: hotBrandAs[i].href,
              imgSrc: imgDom.src,
              title: titleDom.textContent
            })
          }
        }
      } catch(e) {
        console.log('get starProducts occur error')
        console.log(e)
      }
      // console.log(hotBrands)

      // 获取youhuiList
      try{
        let reg = /^\S+\/p\/(\S+)\/$/
        let youhuiMainDom = dom.window.document.querySelector("#youhui-wrapper")
        let youhuiAs = youhuiMainDom.querySelectorAll("li.card-group-list a")
        if (youhuiAs.length > 0) {
          for(let i=0; i<youhuiAs.length; i++) {
            // 获取article_id
            let article_url = youhuiAs[i].href
            let article_id = ''
            let matchs = article_url.match(reg)
            if(matchs && matchs[1]) {
              article_id = matchs[1]
            }
            // 获取img
            let imgDom = youhuiAs[i].querySelector(".zm-card-media img")
            let titleDivs = youhuiAs[i].querySelectorAll(".zm-card-content div")
            let actionsDom = youhuiAs[i].querySelector(".zm-card-content .zm-card-actions")
            let mallDomArr = actionsDom.querySelectorAll(".zm-card-actions-left span span")
            let commentsDomArr = actionsDom.querySelectorAll(".zm-card-actions-right .icon-group")
            youhuiList.push({
              article_url: article_url,
              article_id: article_id,
              article_pic: imgDom.src,
              article_title: titleDivs[0].textContent.trim(),
              article_price: titleDivs[1].textContent.trim(),
              article_mall: mallDomArr[0].textContent.trim(),
              article_date: mallDomArr[1].textContent.trim(),
              article_comment: commentsDomArr[0].textContent.trim(),
              article_worthy: commentsDomArr[1].textContent.trim()
            })
          }
        }
      } catch(e) {
        console.log('get youhuiList occur error')
        console.log(e)
      }

      let menuDetail = {
        titleData,
        categoryTags,
        starProducts,
        hotBrands,
        youhuiList
      };

      resExp.json({
        message: 1,
        data: menuDetail
      })

    });  
  }).on("error", function (err) {  
    Logger.error(err.stack)  
    resExp.json({})
  });  
})


// 点值功能
app.get('/apizdm/evaluate', function (req, res) {
  var url = 'https://zhiyou.m.smzdm.com/user/rating/jsonp_add'
  console.log('点值功能:')
  console.log(req.query)
  axios.get(url, {
    params: req.query,
    headers: {
      Host: 'zhiyou.m.smzdm.com',
      Referer: `https://m.smzdm.com/p/${req.query.article_id}/`
    }
  }).then((response) => {
    var ret = response.data
    if (typeof ret === 'string') {
      var reg = /^\w+\(({.+})\)$/
      var matches = ret.match(reg)
      if (matches) {
        ret = JSON.parse(matches[1])
      }
    }
    res.json(ret)
  }).catch((e) => {
    console.log(e)
  })
})


app.listen(3000, '192.168.1.102');
console.log('listen 3000');
